package com.warframe.fissures;

import com.alibaba.fastjson.JSONArray;
import com.tools.HibernateUtils;
import com.tools.StringContent;
import com.warframe.entity.database.WikiTranslateEO;
import com.warframe.entity.fissures.FissuresMO;
import com.warframe.entity.message.Message;
import com.warframe.message.Executor;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Fissures implements Runnable {

    private String url = "https://api.warframestat.us/pc/fissures";
    private String sql = "select * from WikiTranslate a where a.item_en in (TARGET) order by a.item_cn";
    private Message message;
    private List<FissuresMO> fissuresMOList;
    private StringBuilder formatStr = new StringBuilder();

    public Fissures(Message message) {
        this.message = message;
        fissuresMOList = new ArrayList<>();
    }

    public void getResult() {
        CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建httpClient实例
        HttpGet httpGet = new HttpGet(url); // 创建httpget实例
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
        CloseableHttpResponse response = null; // 执行http get请求
        try {
            response = httpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity(); // 获取返回实体
        String res = null;
        try {
            res = EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }
        fissuresMOList = JSONArray.parseArray(res, FissuresMO.class);
        Collections.sort(fissuresMOList, this::compareASC);
    }

    private int compareASC(FissuresMO o1, FissuresMO o2) {
        if (o1.getTierNum() > o2.getTierNum()) {
            return 1;
        } else if (o1.getTierNum() < o2.getTierNum()) {
            return -1;
        }
        return 0;
    }

    public void format() {
        for (FissuresMO eachMO : fissuresMOList) {
            formatStr.append(eachMO.getMissionType() + " " + eachMO.getTier() + "\n");
            formatStr.append(eachMO.getNode() + " " + eachMO.getEta() + "\n");
        }
    }

    public void explain() {
        List<String> params = new ArrayList<>();
        for (FissuresMO eachMO : fissuresMOList) {
            String node = eachMO.getNode();
            params.add(node.substring(node.indexOf("(") + 1, node.lastIndexOf(")")));
            params.add(eachMO.getMissionType());
            params.add(eachMO.getTier());
        }
        StringBuilder target = new StringBuilder();
        for (String str : params) {
            target.append("'" + str + "'");
        }
        String str = target.toString();
        str = str.replace("''", "','");
        Session session = HibernateUtils.getSessionFactory().openSession();
        SQLQuery sqlQuery = session.createSQLQuery(sql.replace("TARGET", str));
        List<WikiTranslateEO> wikiTranslateEOList = sqlQuery.addEntity(WikiTranslateEO.class).list();
        String resultStr = formatStr.toString();
        for (WikiTranslateEO eachMO : wikiTranslateEOList) {
            resultStr = resultStr.replace(eachMO.getItemEn(), eachMO.getItemCn());
        }
        formatStr = new StringBuilder(resultStr);
        session.close();
    }

    @Override
    public void run() {
        getResult();
        format();
        explain();
        message.setSign(StringContent.SENDPERSON);
        message.setMsg(formatStr.toString());
        Executor.getInstance().pushMsg(message);
    }
}
